package com.lbssoft.gpsbd.controller;

import com.lbssoft.gpsbd.dao.StatisticsStopMapper;
import com.lbssoft.gpsbd.model.Result;
import com.lbssoft.gpsbd.model.StatisticsStop;
import com.lbssoft.gpsbd.model.vo.Counts;
import com.lbssoft.gpsbd.model.vo.UserVo;
import com.lbssoft.gpsbd.service.StatisticsService;
import com.lbssoft.gpsbd.service.StatisticsStopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api("统计分析")
@RestController
@RequestMapping("/statistics")
public class StatisticsController {


	@Autowired
	private StatisticsService statisticsService;

	@Autowired
	private StatisticsStopService statisticsStopService;

	@ApiOperation(value="获取设备统计", notes="获取设备统计")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType="query",name = "start", value = "开始时间", required = true, dataType = "String"),
			@ApiImplicitParam(paramType="query",name = "end", value = "结束时间", required = true, dataType = "String")
	})
	@RequestMapping(value = "/device",method = {RequestMethod.GET})
	@ResponseBody
	public Result device(@NotEmpty @RequestParam(value = "deviceIds", defaultValue = "0") String deviceIds,
			             @NotEmpty @RequestParam(value = "column", defaultValue = "trip") String column,
						 @NotEmpty @RequestParam(value = "start", defaultValue = "") String start,
						 @NotEmpty @RequestParam(value = "end", defaultValue = "") String end) {
		UserVo user = (UserVo) SecurityUtils.getSubject().getPrincipal();
		Result result = new Result();
		List<Map> list = statisticsService.getDeviceStatisicsByUserId(user.getId(),deviceIds,column,start,end);
		result.setData(list);
		return result;
	}

	@ApiOperation(value="统计整体数量", notes="统计用户 设备 报警 围栏数量")
	@RequestMapping(value = "/counts",method = {RequestMethod.GET})
	@ResponseBody
	public Result counts() {
		Result result = new Result();
		Counts counts = statisticsService.getCounts();
		result.setData(counts);
		return result;
	}


	@ApiOperation(value="停留统计", notes="停留统计")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType="query",name = "page", value = "当前页",   dataType = "int"),
			@ApiImplicitParam(paramType="query",name = "pageSize", value = "每页显示的条数",  dataType = "int"),
			@ApiImplicitParam(paramType="query",name = "deviceId", value = "设备id",  dataType = "long")
	})
	@RequestMapping(value = "stop",method = {RequestMethod.GET})
	@ResponseBody
	public Result stopList(@NotEmpty @RequestParam(value = "page", defaultValue = "1") int page,
					   @NotEmpty @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
					   @RequestParam(value = "deviceId", defaultValue = "") long deviceId) {
		return statisticsStopService.getList(deviceId,page,pageSize);
	}

	
}